<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\ContactInfo;
use App\Admin\Repositories\Record;
use App\Jobs\SendSMS;
use App\Models\Contact;
use App\Models\MessageTemplate;
use App\Models\Record as ModelsRecord;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class RecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Record(), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableActions();

            $grid->column('id')->sortable();
            $grid->column('contact_id')->display(function ($v) {
                $result = Contact::find($v);
                return "{$result['name']}[{$result['phone']}]";
            });
            $grid->column('template_id', '短信内容')->display(function ($v) {
                $content = (MessageTemplate::find($v))['content'];
                if (isset($this->params) && $this->params) {
                    $content = str_replace('【变量】', $this->params, $content);
                }
                return $content;
            });
            // $grid->column('params');
            $grid->column('smsid');
            // $grid->column('msg');
            $grid->column('status')->using([1 => '发送中', 2 => '发送成功']);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Record(), function (Show $show) {
            $show->panel()->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
            });

            $show->field('id');
            $show->field('contact_id');
            $show->field('template_id');
            $show->field('params');
            $show->field('smsid');
            $show->field('msg');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Record(), function (Form $form) {
            $form->disableEditingCheck();

            $form->display('id');
            $form->multipleSelectTable('contact_id')->required()
                ->from(ContactInfo::make(['id' => $form->getKey()]));
                // ->options(function ($v) {
                //     if (! $v) return [];
                //     return Contact::find($v)->pluck('phone', 'id');
                // })
                // ->saving(function ($v) {
                //     return implode(',', $v);
                // });
            $form->radio('template_id')->required()
                ->options(MessageTemplate::where(['status' => 2])->pluck('content', 'id'));
            $form->radio('type', '附加内容')->options([0 => '无', 1 => '变量', 2 => '验证码'])
                ->when(1, function(Form $form) {
                    $form->text('params', '变量内容');
                })->default(0)->required();

            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function (Form $form) {
                $type = $form->type;
                $contacts = explode(',', $form->contact_id);

                foreach ($contacts as $key => $value) {
                    $data['contact_id'] = $value;
                    $data['template_id'] = $form->template_id;
                    if ($type == 2) {
                        $data['params'] = rand(000000, 999999);
                    } else {
                        $data['params'] = $form->params;
                    }
                    SendSMS::dispatch($data);
                }
                return $form->response()->success('短信发送完成！');
            });
        });
    }
}
